/**
 * User: yeqingzhou01
 * Date: 13-10-23
 * Time: 上午10:38
 */
var util = require('util');
var errors = require('../../error/errors');
var fs = require('fs');
var readline = require('readline');
var shellUtils = require('../../utils/shell-utils');

exports.readFileLines = function (filePath, curCachedNum, maxCachedNum, isDelete, callback, next) {
    try {
        util.log('[FILE DOC UTILS] Reading file: ' + filePath);
        var fileReadStream = fs.createReadStream(filePath);
        fileReadStream.on('error', function(err){
            console.error('[FILE DOC UTILS][ERROR]: ' + err.message);
        });
        util.log('[FILE DOC UTILS] Reading file line: ');
        var rl = readline.createInterface({
            input: fileReadStream,
            terminal: false
        });
        rl.on('line', function (oneLineStr) {
            callback(oneLineStr);
            if (curCachedNum >= maxCachedNum) {
                util.log('[FILE DOC UTILS][' + curCachedNum + ' DOCS RUNNING] PAUSING reading from file: ' + filePath);
                rl.pause();
            }
        });
        rl.on('pause', function () {
            util.log('[FILE DOC UTILS][' + curCachedNum + ' DOCS RUNNING] PAUSE reading from file: ' + filePath);
//            while(curCachedNum >= maxCachedNum){}
//            rl.resume();
        });
        rl.on('resume', function () {
            util.log('[FILE DOC UTILS][' + curCachedNum + ' DOCS RUNNING] RESUMED FROM PAUSE reading from file: ' + filePath);
        });
        rl.on('close', function () {
            util.log('[FILE DOC UTILS] Stop reading from file: ' + filePath);
            if(next){
                next();
            }
            if (isDelete) {
                exports.deleteFile(
                    filePath,
                    function (msg) {
                        util.log('[FILE DOC UTILS] Deleted file: ' + filePath + ', message: ' + msg);
                    }
                );
            }
        });
        return rl;
    } catch (e) {
        util.error(errors.getErrorInstance(
            errors.errorsName.data,
            e.message
        ).message);
    }
}
exports.deleteFile = function (filePath, callback) {
    fs.unlink(
        filePath,
        callback
    )
}
exports.deleteFileSync = function(filePath){
    try{
        fs.unlinkSync(filePath);
    } catch(e){
        util.log('[FILE DOC UTIL] FILE [' + filePath + '] NOT DELETED: ' + e.message);
    }
}
exports.removeDirSync = function (dirPath) {
    fs.rmdirSync(dirPath);
}
exports.makeDirSync = function (dirPath) {
    fs.mkdirSync(dirPath, true);
}
exports.writeIntoFileSync = function (filePath, inputData, errCallback) {
    if(inputData){
        fs.writeFileSync(filePath, inputData, errCallback);
    }
}
exports.appendFileSync = function (filePath, inputData){
    if(inputData){
	fs.appendFileSync(filePath, inputData)
    }
}
exports.deleteFolderRecursive = function (path) {
    if (fs.existsSync(path)) {
        fs.readdirSync(path).forEach(function (file, index) {
            var curPath = path + "/" + file;
            if (fs.statSync(curPath).isDirectory()) { // recurse
                exports.deleteFolderRecursive(curPath);
            } else { // delete file
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(path);
    }
}
